package hikari.gui.animationeditor;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 * @author sad
 */
public class GraphicDrawer_AnimationDiff extends JPanel {

    double[] data = null;

    public GraphicDrawer_AnimationDiff() {
        setBackground(Color.BLACK);
    }

    public void setData(double[] data) {
        this.data = data;
    }

    private double calculateMax(double[] data) {
        double max = 0;
        for (double d : data) {
            if (d > max) {
                max = d;
            }
        }
        return max;
    }

    public void draw(Graphics g) {
        double max = calculateMax(data);
        double height = getHeight();
        double width = getWidth();
        double increment = width / (double)data.length;
        double x=0;
        double y=0;
        double proportion=height/max;
        g.setColor(Color.WHITE);
        int lastX=0;
        int lastY=0;
        for(double d:data){
            y=d*proportion;
            x+=increment;
            g.drawLine(lastX, lastY, (int)x, (int)y);
            lastX=(int) x;
            lastY=(int) y;
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (data != null) {
            draw(g);
        }
    }
}
